#include <Windows.h>
#include <extend.h>
#include <CommDlg.h>

static WORD wIndex;

//----------------------------------------------------------------------------//

CLIPPER CGETFILE()   // ( cFileMask, cTitle, nDefaultMask, cInitDir, lSave )  --> cFileName
{
   OPENFILENAME ofn;
   char szDirName[ 100 ], szFile[ 256 ], szFileTitle[ 256 ], szFilter[ 300 ];
   char szTitle[ 100 ];
   WORD w = 0;
   BYTE bIndex = _parni( 3 );
   BOOL bSave = _parl( 5 );

   _strcpy( szDirName, IF( _parc( 4 ), _parc( 4 ), ".\\" ) );
   _strcpy( szFilter, _parc( 1 ) );

   _strcpy( szFile, _parc( 1 ) );
   _strcpy( szTitle, IF( _ischar( 2 ), _parc( 2 ), "" ) );

   // _xunlock();

   while( * ( szFilter + w ) )
   {
      if( * ( szFilter + w ) == '|' )
      {
         * ( szFilter + w ) = 0;
         szFile[ 0 ] = 0;
      }
      w++;
   }
   szFilter[ w++ ] = 0;
   szFilter[ w++ ] = 0;
   szFilter[ w   ] = 0;

   _bset( ( char * ) &ofn, 0, sizeof( OPENFILENAME ) );

   ofn.lStructSize     = sizeof( OPENFILENAME );
   ofn.hwndOwner       = GetActiveWindow();
   ofn.lpstrFilter     = szFilter;
   ofn.nFilterIndex    = bIndex ? bIndex: 1;
   ofn.lpstrFile       = szFile;
   ofn.nMaxFile        = sizeof( szFile );
   ofn.lpstrFileTitle  = szFileTitle;
   ofn.nMaxFileTitle   = sizeof( szFileTitle );
   ofn.lpstrInitialDir = szDirName;
   ofn.lpstrTitle      = szTitle;
   ofn.Flags           = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | // OFN_LONGNAMES |
                        ( bSave ? OFN_HIDEREADONLY : 0 ) ;

   wIndex = 0;

   if( bSave )
   {
      if( GetSaveFileName( &ofn ) )
         _retc( szFile );
      else
         _retc( "" );
   }
   else
   {
      if( GetOpenFileName( &ofn ) )
         _retc( szFile );
      else
         _retc( "" );
   }


   wIndex = ofn.nFilterIndex;
}

//----------------------------------------------------------------------------//

CLIPPER NGETFILEFI()   //  LTER()    Returns the index of the filter selection
{
   _retni( wIndex );
}

//----------------------------------------------------------------------------//
